package com.sservice.web.controller.admin;

import com.sservice.common.CompareObject;
import com.sservice.common.Constants;
import com.sservice.common.SameUrlData;
import com.sservice.service.service.IPayTypeService;
import com.sservice.service.service.ISysLogService;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.model.entity.PayType;
import com.sservice.urp.entity.User;
import com.sservice.web.log.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 */

@Controller
@RequestMapping("/admin/payType")
public class PayTypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IPayTypeService service;
    @Autowired
    private ISysLogService sysLogService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(PayType arg, HttpServletRequest request) {
        if (!"0".equals(arg.getType())) {
            User user = (User) request.getSession().getAttribute(Constants.USER);
            arg.setComId(String.valueOf(user.getComid()));
        }
        List service_res = service.queryAll(arg);
        ListResult<PayType> res = new ListResult(true, service_res);
        return res;
    }

    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ListResult create(PayType arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        int service_res = service.insertSelective(arg);
        User user = (User) request.getSession().getAttribute(Constants.USER);
        arg.setComId(String.valueOf(user.getId()));
        if (service_res > 0) {
            sysLogService.log("创建", request, Integer.valueOf(arg.getId()), Action.payTypeType);
        }
        ListResult<PayType> res = new ListResult(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ListResult update(PayType arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        PayType pt = service.selectByPrimaryKey(arg.getId());
        int service_res = service.updateByPrimaryKeySelective(arg);
        if (service_res > 0) {
            Map<String, Object> res = CompareObject.contrastObj(arg, pt);
            sysLogService.log(res, request, Integer.valueOf(arg.getId()), Action.payTypeType);
        }
        ListResult<PayType> res = new ListResult(true, service_res);
        return res;
    }

}
